package com.example.question.tree.tree2;

import com.example.question.tree.tree1.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class Code07_113 {
    public static void main(String[] args) {

    }

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> ret = new ArrayList<>();
        dfs(root, targetSum, 0, new ArrayList<>(), ret);
        return ret;
    }

    private void dfs(TreeNode node, int targetSum, int sum, List<Integer> path, List<List<Integer>> ret) {
        if (node == null) {
            return;
        }
        path.add(node.val);
        if (node.left == null && node.right == null && sum + node.val == targetSum) {
            ret.add(new ArrayList<>(path));
        }
        dfs(node.left, targetSum, sum + node.val, path, ret);
        dfs(node.right, targetSum, sum + node.val, path, ret);
        path.remove(path.size() - 1);
    }
}
